"""
作者: 李聪
日期: 2021年 12月 03日
"""


def conflict(state, nextColumn):
    nextRow = rows = len(state)  # 5
    for row in range(rows):  # 0,1,2,3,4
        column = state[row]
        if abs(column - nextColumn) in [0, nextRow - row]:
            return True
    return False
def queens(num, state=()):
    for pos in range(num):
        if not conflict(state, pos):
            if len(state) == num - 1:
                yield (pos,)
            else:
                for result in queens(num, state + (pos,)):
                    yield (pos,) + result


def prettyprint(solution):
    def line(pos, length=len(solution)):
        return '.' * (pos) + 'X' + '.' * (length - pos - 1)

    for pos in solution:
        print(line(pos))


if __name__ == '__main__':
    solutions = queens(8)
    for index, solution in enumerate(solutions):
        print('第%d种解决方案：' % (index + 1), solution)
        prettyprint(solution)
        print('*' * 50)